package com.example.safeassistan.train.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.train.adapter.ActivityReportCardAdapter;
import com.example.safeassistan.train.adapter.TrainSimulateListAdapter;
import com.example.safeassistan.train.model.ReportCardModel;
import com.example.safeassistan.train.model.TrainTestListModel;
import com.example.safeassistan.utile.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.example.safeassistan.common.MethodUtils.getUserId;
import static com.example.safeassistan.constants.Constants.PAGESIZE;

/***
 * 成绩单
 ***/

public class ActivityreportCard  extends BaseActivity {
    private RecyclerView rcView;
    private ActivityReportCardAdapter adapter;
    private String paperId="";//试卷id
    private TextView tvMyScore,tvTotalScore,tvTestCount,tvUnTestCount;
    private  ReportCardModel model;
    private TextView tvQualifiedCount;

    public static void actionStart(Context context,String paperId) {
        Intent intent = new Intent(context, ActivityreportCard.class);
        intent.putExtra("paperId",paperId);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_report_card;
    }

    @Override
    protected void initView() {
        setTitleText("成绩单");
        paperId=getIntent().getStringExtra("paperId");
        rcView=findViewById(R.id.rcView);
        tvMyScore=findViewById(R.id.tvMyScore);
        tvTotalScore=findViewById(R.id.tvTotalScore);
        tvQualifiedCount=findViewById(R.id.tvQualifiedCount);
        tvTestCount=findViewById(R.id.tvTestCount);
        tvUnTestCount=findViewById(R.id.tvUnTestCount);

        getData();
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }


    public void getData(){
        new HttpRequest(ActivityreportCard.this).doGet(UrlConstants.getTestForm+paperId, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    int code = jsonObject.getInt("code");
                    if (code == 200) {
                        Object obj = JsonUtils.fromJson(result.toString(), ReportCardModel.class);
                        if (obj instanceof ReportCardModel) {
                             model=(ReportCardModel) obj;
                            setView();
                        }
                    }else {
                        Toast.makeText(ActivityreportCard.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    public void setView(){
        tvMyScore.setText((model.getResult().getMaxScore()==null?"0":model.getResult().getMaxScore())+"");
        tvTotalScore.setText("/"+(model.getResult().getTotalScore()==null?"0":model.getResult().getTotalScore()));
        tvQualifiedCount.setText((model.getResult().getQualifiedCount()==null?"0":model.getResult().getQualifiedCount())+"");
        tvTestCount.setText((model.getResult().getTotalCount()==null?"0":model.getResult().getTotalCount())+"");
        tvUnTestCount.setText((model.getResult().getUnCount()==null?"0":model.getResult().getUnCount())+"");
        adapter=new ActivityReportCardAdapter(model.getResult().getHistoryList());
        rcView.setLayoutManager(new LinearLayoutManager(getContext()));
        rcView.setAdapter(adapter);
    }
}